<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %> 
<%!
Color getRandColor(int fc,int bc){//给定范围获得随机颜色
        Random random = new Random();
        if(fc>255) fc=255;
        if(bc>255) bc=255;
        int r=fc+random.nextInt(bc-fc);
        int g=fc+random.nextInt(bc-fc);
        int b=fc+random.nextInt(bc-fc);
        return new Color(r,g,b);
        }
%>
<%

	    int width=60;
	    int height=18;
	    
        BufferedImage buffImg=new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g=buffImg.createGraphics();
        
        Random random=new Random();
        
        g.setColor(Color.WHITE);
        g.fillRect(0,0,width,height);
        
        Font font=new Font("Times New Roman",Font.BOLD,18);
        g.setFont(font);
        
        g.setColor(Color.BLACK);
        g.drawRect(0,0,width-1,height-1);
        
        g.setColor(Color.gray);
        for (int i=0;i<80;i++)
        {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x,y,x+xl,y+yl);
        }
        
        StringBuffer randomCode=new StringBuffer();
        int red=0,green=0,blue=0;
        
        for (int i=0;i<4;i++)
        {
            String strRand=String.valueOf(random.nextInt(10));
            red=random.nextInt(110);
            green=random.nextInt(50);
            blue=random.nextInt(50);
            g.setColor(new Color(red,green,blue));
            g.drawString(strRand,13*i+6,16);
            randomCode.append(strRand);
        }
        session=request.getSession();
        session.setAttribute("systemRandomCode",randomCode.toString());		// 将验证码保存在 session 中
        // session.setAttribute("LoginRandomCode",randomCode.toString());
        
        response.setHeader("Pragma","no-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        ServletOutputStream sos=response.getOutputStream();
        ImageIO.write(buffImg, "jpeg",sos);
        
        // 以下三行是为了防止报错
        sos.close();
        out.clear();
	    out = pageContext.pushBody();
%>



